import socket
import sys

sys.path.append("../../")
from zinx.znet.datapack import NewDataPack
from zinx.znet.message import NewMessage

if __name__ == '__main__':
    HOST = '127.0.0.1'  # 服务器IP地址
    PORT = 8986  # 服务器端口号
    # 创建socket对象
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    # 连接服务器
    s.connect((HOST, PORT))
    print(f"Connected to {HOST}:{PORT}")
    # 发送数据
    data = "Hello, Server"
    msg = NewMessage(1, len(data), data.encode("utf-8"))
    dp = NewDataPack()
    send_data = dp.Pack(msg)
    s.sendall(send_data)
    # 接收响应数据
    response = s.recv(1024)
    print("Received from server: ", response)
    # 关闭连接
    s.close()
